﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TahsilatGirisi
{
    public partial class frmAyar : Form
    {
        public frmAyar()
        {
            InitializeComponent();
        }

        private void frmAyar_Load(object sender, EventArgs e)
        {
            this.VerileriOku();
            this.txtOkunacakServer.Focus();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.VerileriYaz();
            Application.Exit();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void VerileriOku()
        {
            if (File.Exists(Application.StartupPath + @"\Setting.cfg"))
            {
                StreamReader reader = new StreamReader(Application.StartupPath + @"\Setting.cfg");
                this.txtOkunacakServer.Text = reader.ReadLine();
                this.txtOkunacakKullanici.Text = reader.ReadLine();
                this.txtOkunacakSifre.Text = reader.ReadLine();
                this.cmbOkunacakDb.Text = reader.ReadLine();
                reader.ReadLine();
                reader.ReadLine();
                this.textBox1.Text = reader.ReadLine();
                reader.Close();
            }
        }

        private void VerileriYaz()
        {
            StreamWriter writer = new StreamWriter(Application.StartupPath + @"\Setting.cfg");
            writer.WriteLine(this.txtOkunacakServer.Text);
            writer.WriteLine(this.txtOkunacakKullanici.Text);
            writer.WriteLine(this.txtOkunacakSifre.Text);
            writer.WriteLine(this.cmbOkunacakDb.Text);
            writer.WriteLine(this.checkBox1.Checked);
            string str = "";
            if (this.checkBox1.Checked)
            {
                str = "Server=" + this.txtOkunacakServer.Text + ";Database=" + this.cmbOkunacakDb.Text + ";Trusted_Connection=True;";
            }
            else
            {
                str = "Data Source=" + this.txtOkunacakServer.Text + ";Initial Catalog=" + this.cmbOkunacakDb.Text + ";User Id=" + this.txtOkunacakKullanici.Text + ";Password=" + this.txtOkunacakSifre.Text + ";Application Name=LOGMANAGER";
            }
            writer.WriteLine(str);
            writer.WriteLine(this.textBox1.Text);
            writer.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionString = "";
                if (this.checkBox1.Checked)
                {
                    connectionString = "Server=" + this.txtOkunacakServer.Text + ";Database=" + this.cmbOkunacakDb.Text + ";Trusted_Connection=True;";
                }
                else
                {
                    connectionString = "Data Source=" + this.txtOkunacakServer.Text + ";Initial Catalog=" + this.cmbOkunacakDb.Text + ";User Id=" + this.txtOkunacakKullanici.Text + ";Password=" + this.txtOkunacakSifre.Text;
                }
                SqlConnection connection = new SqlConnection(connectionString);
                SqlCommand selectCommand = new SqlCommand("SELECT name FROM  sysdatabases where dbid>5")
                {
                    Connection = connection
                };
                SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                this.cmbOkunacakDb.DataSource = dataTable;
                this.cmbOkunacakDb.DisplayMember = "NAME";
                this.cmbOkunacakDb.ValueMember = "NAME";
            }
            catch (SqlException exception)
            {
                MessageBox.Show("Connection Failler. " + exception.Message, "Uyari", MessageBoxButtons.OK);
            }
        }
    }
}
